package com.hs.mod_archive.activity

import android.content.BroadcastReceiver
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.findNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.hs.common.RouteIndex
import com.hs.common.UserInitInfo
import com.hs.common.dto.SysDeptDto
import com.hs.common.dto.SysRoleDto
import com.hs.common.repository.IDeptRepository
import com.hs.common.repository.IRoleRepository
import com.hs.common.repository.IUserRepository
import com.hs.framework.base.BaseDataBindActivity
import com.hs.framework.utils.StatusBarSettingHelper
import com.hs.mod_archive.Const
import com.hs.mod_archive.R
import com.hs.mod_archive.ScheduleService
import com.hs.mod_archive.databinding.ActivityCabinetBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@Route(path = RouteIndex.activityCabinet)
@AndroidEntryPoint
class ActivityCabinet : BaseDataBindActivity<ActivityCabinetBinding>() {


    private lateinit var receiver: BroadcastReceiver

    @Inject
    lateinit var userService: IUserRepository

    @Inject
    lateinit var roleService: IRoleRepository

    @Inject
    lateinit var deptService: IDeptRepository

    override fun initView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        val i = Intent(this, ScheduleService::class.java)
        startService(i)
//        val displayMetrics = DisplayMetrics()
//        // 获取窗口管理器服务
//        // 获取窗口管理器服务
////        某手机的屏幕分辨率是 1080*1920，屏幕像素密度是 480 dpi，通过计算
////        它的最大宽度对应的 dp 值是:1080/(480/160)=360 dp
////        根据这个 368 dp 值，系统会去寻找 value-sw36edp 的文件夹以及对应的资源文件并使用它
////        如果没有 value-sw36edp文件夹，系统会向下寻找，比如离 360 dp最近的只有 value-sw350dp，那么Android就会选择 value-sw350dp
////        文件夹下面的资源文件。
//
//        this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics)
//        val widthPixels = displayMetrics.widthPixels
//        val heightPixels = displayMetrics.heightPixels
//        val density = displayMetrics.density
//        val densityDpi = displayMetrics.densityDpi
//        Log.d("ScreenResolution", "屏幕宽度（像素）: $widthPixels")  //1024
//        Log.d("ScreenResolution", "屏幕高度（像素）: $heightPixels") // 504
//        Log.d("ScreenResolution", "屏幕密度: $density") // 2.0
//        Log.d("ScreenResolution", "屏幕密度DPI: $densityDpi") // 320
        initUserInfo()
        receiver = object : BroadcastReceiver() {
            override fun onReceive(context: android.content.Context?, intent: Intent) {
                callback()
            }
        }
        registerReceiver(receiver, IntentFilter(Const.ACT_ACTIVITY_DETECTION))
    }

    //返回上一层
    fun callback() {
//        ARouter.getInstance().build(RouteIndex.activityCabinet).navigation()
        val navController: NavController = findNavController(R.id.nav_host_fragment)
        navController.navigateUp()
    }


    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receiver)
    }

    private fun initUserInfo() {

        lifecycleScope.launch(Dispatchers.IO) {
            UserInitInfo.roleMap.forEach { (k, v) ->
                run {
                    roleService.selectByCode(UserInitInfo.sysTemNo) ?: run {
                        val role =
                            SysRoleDto(roleName = k, code = v, sysTemNo = UserInitInfo.sysTemNo)
                        roleService.insertRole(role)
                    }
                }
            }
        }

        lifecycleScope.launch(Dispatchers.IO) {
            UserInitInfo.deptMap.forEach { (k, v) ->
                run {
                    deptService.selectByCode(UserInitInfo.sysTemNo) ?: run {
                        val dept =
                            SysDeptDto(deptName = k, code = v, sysTemNo = UserInitInfo.sysTemNo)
                        deptService.insertDept(dept)
                    }
                }
            }
        }

        lifecycleScope.launch(Dispatchers.IO) {
            userService.initUser(UserInitInfo.sysTemNo)
        }


    }
}